package org.example.controller;

import org.example.pojo.UserProfile;
import org.example.service.impl.UserProfileServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;


//
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("userProfile")
//@CrossOrigin(origins = "http://localhost:5173") // 允许来自http://localhost:5173的请求
public class UserProfileController {
    @Autowired
    private UserProfileServiceImpl dao;
    @RequestMapping("findall")
    public UserProfile findall(int id){
        String resumeUrl = dao.findall(id).getResumeUrl();
        System.err.println(resumeUrl);
        return dao.findall(id);
    }
    @RequestMapping("add")
    public int add(UserProfile up){
        return dao.add(up);
    }
    @RequestMapping("upd")
    public int uod(UserProfile userProfile){
        return dao.upd(userProfile);
    }



    //上传简历
    @PostMapping("pian")
    public String pian(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        String poth="C:\\Users\\Administrator\\Desktop\\项目\\digital-interviewer-vue\\src\\jianli";

        File f=new File(poth,originalFilename);
        file.transferTo(f);

        String url="http://localhost:8992/user/jianli/"+originalFilename;
        System.err.println(url);
        return url;
    }




//    @Value("${file.upload.path}")
//    private String uploadPath;

    // 文件上传接口
//    @PostMapping("/upload")
//    public String uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
//        if (file.isEmpty()) {
//            return "文件为空，请选择文件";
//        }
//        // 生成唯一文件名（避免重复）
//        String originalFilename = file.getOriginalFilename();
//        String fileExtension = originalFilename.substring(originalFilename.lastIndexOf("."));
//        String newFileName = UUID.randomUUID().toString() + fileExtension;
//        File destFile = new File(uploadPath, newFileName);
//        file.transferTo(destFile);
//        // 返回文件查看路径（基于当前端口）
//        return "/view/" + newFileName;
//    }

//    @PostMapping("/userProfile/upload")
//    public String upload(@RequestParam("file") MultipartFile file) throws IOException {
//        // 上传文件逻辑
//        return "上传成功";
//    }

    // 文件查看接口
//    @GetMapping("/view/{fileName}")
//    public ResponseEntity<Resource> viewFile(@PathVariable String fileName) {
//        File file = new File(uploadPath, fileName);
//        if (!file.exists()) {
//            return ResponseEntity.notFound().build();
//        }
//        Resource resource = new FileSystemResource(file);
//        HttpHeaders headers = new HttpHeaders();
//        headers.add(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=" + fileName);
//        return ResponseEntity.ok()
//                .headers(headers)
//                .contentType(MediaType.APPLICATION_OCTET_STREAM)
//                .body(resource);
//    }
}
